package com.primecodegen.common.DataModelObject;

import com.primecodegen.common.DataModelProject.DataModelProjectDAO;
import com.primecodegen.common.DataModelProject.DataModelProjectTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;

@ManagedBean(name = "dataModelObjectBean")
@RequestScoped
public class DataModelObjectBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(DataModelObjectBean.class.getName());
    private int count;
    private int dataModelObjectID;
    private String excludedClmn;
    private DataModelObjectTable selectedDataModelObject;
    HttpSession session;
    private DataModelObjectDAO dataModelObjectDAO;
    private ArrayList<DataModelObjectTable> dataModelObjectList;
    private DataModelObjectTable dataModelObjectSrchCrit;
    private DataModelObjectTable dataModelObjectUpdate;
    private DataModelObjectTable dataModelObjectTable;
    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public DataModelObjectBean() {
        logger.info(" Constructor DataModelObjectBean()");
        excludedClmn = "6";
        context = getContext();
        session = getSession();
        dataModelObjectDAO = new DataModelObjectDAO();
        dataModelObjectList = new ArrayList<DataModelObjectTable>();
        dataModelObjectTable = new DataModelObjectTable();
        dataModelObjectSrchCrit = (DataModelObjectTable) session.getAttribute("sessionDataModelObjectSrchCrit");
        dataModelObjectUpdate = (DataModelObjectTable) session.getAttribute("dataModelObjectUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (dataModelObjectSrchCrit == null) {
            dataModelObjectSrchCrit = new DataModelObjectTable();
        }

        if (dataModelObjectUpdate == null) {
            dataModelObjectUpdate = new DataModelObjectTable();
        }



        fillDataModelObjectList();
    }

    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public DataModelObjectTable getDataModelObjectUpdate() {
        dataModelObjectUpdate = (DataModelObjectTable) session.getAttribute("dataModelObjectUpdateSess");
        if (dataModelObjectUpdate == null) {
            dataModelObjectUpdate = new DataModelObjectTable();
        }
        return dataModelObjectUpdate;
    }

    public void setDataModelObjectUpdate(DataModelObjectTable dataModelObjectUpdate) {
        session.setAttribute("dataModelObjectUpdateSess", dataModelObjectUpdate);
        this.dataModelObjectUpdate = dataModelObjectUpdate;
    }

    public DataModelObjectBean(DataModelObjectTable dataModelObjectTable) {
        logger.info("Constructor DataModelObjectBean( DataModelObjectTable dataModelObjectTable )");
        this.dataModelObjectTable = dataModelObjectTable;
    }

    public DataModelObjectTable getDataModelObjectTable() {
        return dataModelObjectTable;
    }

    public void setDataModelObjectTable(DataModelObjectTable dataModelObjectTable) {
        this.dataModelObjectTable = dataModelObjectTable;
    }

    public DataModelObjectTable getDataModelObjectSrchCrit() {
        return dataModelObjectSrchCrit;
    }

    public void setDataModelObjectSrchCrit(DataModelObjectTable dataModelObjectSrchCrit) {
        this.dataModelObjectSrchCrit = dataModelObjectSrchCrit;
    }

    public DataModelObjectTable getSelectedDataModelObject() {
        return selectedDataModelObject;

    }

    public void setSelectedDataModelObject(DataModelObjectTable selectedDataModelObject) {
        this.selectedDataModelObject = selectedDataModelObject;
    }

    public ArrayList<DataModelObjectTable> getDataModelObjectList() {
        return dataModelObjectList;
    }
//******************************
    private ArrayList<SelectItem> dataModelProjectList = new ArrayList<SelectItem>();
    private DataModelProjectDAO dataModelProjectDAO = new DataModelProjectDAO();

    public ArrayList<SelectItem> getDataModelProjectList() {
        getAllDataModelProject();
        return dataModelProjectList;
    }

    public void setDataModelProjectList(ArrayList<SelectItem> dataModelProjectList) {
        this.dataModelProjectList = dataModelProjectList;
    }

    public void getAllDataModelProject() {
        dataModelProjectList = new ArrayList<SelectItem>();
        DataModelProjectTable groupTable = new DataModelProjectTable();
        dataModelProjectList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itDataModelProject = dataModelProjectDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itDataModelProject.hasNext()) {
                groupTable = (DataModelProjectTable) itDataModelProject.next();
                dataModelProjectList.add(new SelectItem(groupTable.getID(), groupTable.getCode()));
                groupTable = new DataModelProjectTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllDataModelProject() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in DataModelObjectBean");
        try {
            dataModelObjectSrchCrit.setFilter(true);
            this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(dataModelObjectSrchCrit, null);

            session.setAttribute("sessionDataModelObjectSrchCrit", dataModelObjectSrchCrit);
            fillDataModelObjectList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in DataModelObjectBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

    public void clearFilter() {
        try {
            dataModelObjectSrchCrit = null;
            session.setAttribute("sessionDataModelObjectSrchCrit", dataModelObjectSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillDataModelObjectList ()");

        try {

            if (dataModelObjectSrchCrit != null && colToOrderBy != null) {
                this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(dataModelObjectSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dataModelObjectSrchCrit != null) {
                this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(dataModelObjectSrchCrit, null);
            } else {
                this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDataModelObjectList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("dataModelObjectIDSess").toString());
            dataModelObjectUpdate.setID(parID);
            dataModelObjectDAO.updateByID(dataModelObjectUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = dataModelObjectDAO.insert(dataModelObjectTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "DataModelObject Added"));
                fillDataModelObjectList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in dataModelObjectDAO.insert( dataModelObjectTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("dataModelObjectIDSess").toString());



            dataModelObjectDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "DataModelObject Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("dataModelObjectID"));
            dataModelObjectUpdate = (DataModelObjectTable) dataModelObjectDAO.getByID(parID);
            session.setAttribute("dataModelObjectUpdateSess", dataModelObjectUpdate);
            session.setAttribute("dataModelObjectIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(DataModelObjectBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillDataModelObjectList() {
        logger.log(Level.INFO, "Starting: public void fillDataModelObjectList () in DataModelObjectBean");
        try {

            if ((dataModelObjectSrchCrit != null && dataModelObjectSrchCrit.isFilter()) && colToOrderBy != null) {
                this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(dataModelObjectSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dataModelObjectSrchCrit != null && dataModelObjectSrchCrit.isFilter()) {
                this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(dataModelObjectSrchCrit, null);
            } else {
                this.dataModelObjectList = dataModelObjectDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillDataModelObjectList () in DataModelObjectBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDataModelObjectList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }
}
